import 'package:flustars/flustars.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:zrjz/channel/app_channel.dart';
import 'package:zrjz/comm/constants/app_constant.dart';
import 'package:zrjz/comm/utils/device_utils.dart';
import 'package:zrjz/comm/utils/location_util.dart';
import 'package:zrjz/comm/utils/log_utils.dart';
import 'package:zrjz/comm/utils/util.dart';
import 'package:zrjz/comm/widget/dialog/privacy_policy_dialog/privacy_policy_dialog.dart';
import 'package:zrjz/comm/widget/dialog/privacy_policy_dialog/simple_model_dialog.dart';
import 'package:zrjz/jpush/jpush_receive.dart';
import 'package:zrjz/modules/main/main_page.dart';
import 'package:zrjz/router/page_router.dart';
import 'package:zrjz/services_injection.dart';
import 'package:uuid/uuid.dart';

class SplashController extends GetxController {

  @override
  void onReady() {
    super.onReady();

    //是否是第一次进入App,需要同意协议弹窗，否则退出App
    final isFirstOpen = SpUtil.getBool(AppConstant.storageFirstOpen) ?? false;
    Log.d('isFirstOpen:$isFirstOpen');
    if (!isFirstOpen) {
      //展示协议弹窗
      _showAgreementDialog();
      if (Device.isAndroid) {
        AppChannel.skipAndroidSplashScreen();
      }
    } else {
      _gotoNextPage();
    }
  }

  // 展示协议弹窗
  void _showAgreementDialog() {
    SmartDialog.show(
      usePenetrate: false,
      clickMaskDismiss: false,
      backDismiss: false,
      keepSingle: true,
      builder: (context) => PrivacyPolicyDialog(
        confirmAction: () {
          Log.d('同意协议了');
          SpUtil.putBool(AppConstant.storageFirstOpen, true);
          _gotoNextPage();
        },
        cancelAction: () {
          //提示是否进入基础功能
          _showSimpleModelDialog();
        },
      ),
    );
  }

  // 展示基础功能弹窗
  void _showSimpleModelDialog() {
    SmartDialog.show(
      usePenetrate: false,
      clickMaskDismiss: false,
      backDismiss: false,
      keepSingle: true,
      builder: (context) => SimpleModelDialog(
        confirmAction: () {
          Log.d('同意协议了');
          SpUtil.putBool(AppConstant.storageFirstOpen, true);
          _gotoNextPage();
        },
        cancelAction: () {
          //进入基础功能
          Log.d('进入基础功能');
        },
      ),
    );
  }

  /// 同意了协议之后的初始化第三方与真正跳转逻辑判断
  void _gotoNextPage() async {
    // 路径管理工具类 (SD卡，沙盒等路径获取)
    setInitDir(initTempDir: true, initAppDocDir: true, initAppSupportDir: true, initStorageDir: false);

    await Future.wait([
      Future.delayed(const Duration(milliseconds: 1000)), //默认1秒延时
      DirectoryUtil.getInstance(), //SD卡管控

      FlutterBugly.init(
        androidAppId: "12345678",
        iOSAppId: "12345678",
      ), //Bugly的初始化
      Future(() {
        //注入全局单例Service(内部包含 AppInfo，DeviceInfo 等初始化，并注入了 ConfigService 配置服务类)
        ServicesInjection.init();

        //极光推送初始化与监听
        if (Device.isIOS || Device.isAndroid) {
          jpush.init();
        }

        //初始化定位


      }),
    ]);

    // 先查询原生平台有没有保存需要跳转的子路由
    String routerName = await AppChannel.getNativeRouterName();
    Log.d('SplashController - 查询原生平台有没有保存需要跳转的子路由:$routerName');

    if (!Utils.isEmpty(routerName)) {
      // 只跳转一次
      Get.offAllNamed(routerName);
      AppChannel.clearNativeRouterName();
    } else {
      // 如果没有经历过GUIDE页面，进入GUIDE，否则进入首页
        MainPage.startWithPopAll();
    }

    //跳转页面之后清除 Android 的 SplashScreen 库
    if (Device.isAndroid) {
      AppChannel.skipAndroidSplashScreen();
    }
  }
}
